/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fctu.simulator;

/**
 *
 * @author Fazley Rabbi
 */

import com.hitechbangla.fctu.base.GameScene;
import com.hitechbangla.fctu.base.MeasureHelper;
import com.hitechbangla.fctu.base.aircraft.Aircraft;
import com.hitechbangla.fctu.base.base.Base;
import com.hitechbangla.fctu.base.base.Gbad;
import com.hitechbangla.fctu.base.base.Kpi;
import com.hitechbangla.fctu.base.map.FCTUMap;
import com.hitechbangla.fctu.base.map.WholeMap;
import com.hitechbangla.fctu.base.radar.Radar;
import com.hitechbangla.fctu.form.Addcom;
import com.hitechbangla.fctu.form.AircraftTools;
import com.hitechbangla.fctu.form.CGI;
import com.hitechbangla.fctu.form.CommandAircraftNew;
import com.hitechbangla.fctu.form.CreateForm;
import com.hitechbangla.fctu.form.Gamecontrol;
import com.hitechbangla.fctu.form.RadarTools;
import com.hitechbangla.fctu.form.Utility;
import com.hitechbangla.fctu.form.Voicechat;
import com.hitechbangla.fctu.utility.connection.FCTUClient;
import com.hitechbangla.fctu.utility.connection.GetClientsConnection;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*; 
import java.awt.geom.AffineTransform;
import java.util.HashMap;

import com.hitechbangla.fctu.utility.connection.FCTUClient;
import com.hitechbangla.fctu.utility.connection.GetClientsConnection;
import java.io.Serializable;
import java.util.HashMap;
import org.openstreetmap.gui.jmapviewer.Coordinate;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.MapMarkerDot;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;



public class HTBMain extends JFrame implements ActionListener{
    HTB_MAIN board;
    
    JDesktopPane desk;
    JDesktopPane deskMenu;
    JInternalFrame iframe;
    
    
   String servername = "10.10.2.5"; 
   // if Network is connected
    public boolean isNetworkConnected = false;
    // if it is the game starter client
    public boolean isGameStarter = false;
    // if gamestarter then push values with FCTUClient
    public FCTUClient fctuClient = null;
    // if not a gamestarter then push values with GetClientsConnection
    public GetClientsConnection getClientConnection = null;
    
    
   private int nextFrameX;
   private int nextFrameY;
   private int frameDistance;
   
   
    static int screen_height;
    static int screen_width;
    
    static JMenuBar mainMenuBar;
    static JMenu menuFile;
    static JMenu menuView;
    static JMenu menuHelp;
    
    static JCheckBoxMenuItem aircraft, radarToolsMenu, create, utility, cgi, gamecontrol, aircraftTools, voice;
    static JMenuItem  saveGS, loadGS, exit;
    //mumu
    JMenu windowMenu;
    JMenuItem tileItem;
    
    //rabbi
    static JMenuItem openConnection;
    static JMenuItem lookupConnection;
    //rabbi end
    //commented 17.4
//    JMenuItem menuItemAdd;
//    JMenuItem menuItemRadarTools;
//    JMenuItem menuItemCreate;
//    JMenuItem menuItemUtility;
//    JMenuItem menuItemGeofigure;
//    JMenuItem menuItemGamecontrol;
//    JMenuItem menuItemAircraftTools;
//    JMenuItem menuItemVoice;
//    JMenuItem menuExit;
//    JMenuItem openConnection;
//    JMenuItem lookupConnection;
    //mumu for new menu
    CGI cgiForm;
    Addcom addCom;
    CreateForm createForm;
    Utility ut;
    RadarTools radarTools;
    Gamecontrol frameGameControl;
    AircraftTools frameAircraftTools;
    Voicechat vc;
    public static GameScene gameScene;
    
    public static int openFrameCount;
    
    public HTBMain() {
        
        Toolkit tk = Toolkit.getDefaultToolkit();
        int xSize = ((int) tk.getScreenSize().getWidth());
        int ySize = ((int) tk.getScreenSize().getHeight());
        screen_width = xSize;
        screen_height = ySize;
        
        this.setLayout(null);
        
        
       
        
        desk = new JDesktopPane();
        desk.setBounds( (int)((.2)*screen_width), 0, ((int)(screen_width-5) - (int)((.2)*screen_width)), (int)(screen_height-100));
        iframe = new JInternalFrame("Simulator", false, false, false, false);
        iframe.setBounds(0, 0, desk.getWidth(), desk.getHeight());
        ((javax.swing.plaf.basic.BasicInternalFrameUI) iframe.getUI()).setNorthPane(null);
        //iframe.setBounds( (int)((.2)*screen_width), 0, ((int)(screen_width-5) - (int)((.2)*screen_width)), (int)(screen_height-100));
        
        iframe.setVisible(true);
        iframe.setBorder(null);
        iframe.putClientProperty("JDesktopPane.dragMode", "outline");
        //iframe.addInternalFrameListener(this);
        
        
        
        deskMenu = new JDesktopPane();
        deskMenu.setBounds(0, 0, (int)((.2)*screen_width), (int)(screen_height-100));
        
        
        
        
        try {
            board = new HTB_MAIN(iframe);
           
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        
        iframe.add(board);
        desk.add(iframe);        
        
        createHTBMenu(); 
        
        
        this.setSize(xSize, ySize);
        this.add(desk);
        this.add(deskMenu);
        
        
        this.setVisible(true);
        this.setExtendedState(Frame.MAXIMIZED_BOTH);  
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
//        HTBMain.cascade(deskMenu);
        gameScene = new GameScene();
        gameScene.HTBParent =board;
        try {
            board.start_program();
        }
        catch(Exception ex)
        {
            
        }
        
       
        
    }
    
    public void createHTBMenu() {
        mainMenuBar = new JMenuBar();
        menuFile = new JMenu("File");
        menuView = new JMenu("View");
        menuHelp = new JMenu("Help");
        
        //mumu
         windowMenu = new JMenu("Window");
        
    
//        menuItemAdd = new JMenuItem("Add");
//        menuItemAdd.addActionListener(this);
//
//        menuItemRadarTools = new JMenuItem("Radar Tools");
//        menuItemRadarTools.addActionListener(this);
//
//        menuItemCreate = new JMenuItem("Create");
//        menuItemCreate.addActionListener(this);
//
//        menuItemUtility = new JMenuItem("Utility");
//        menuItemUtility.addActionListener(this);
//
//        menuItemGeofigure = new JMenuItem("Geometric Figure");
//        menuItemGeofigure.addActionListener(this);
//
//        menuItemGamecontrol = new JMenuItem("Game Control");
//        menuItemGamecontrol.addActionListener(this);
//
//        menuItemAircraftTools = new JMenuItem("Aircraft Tools");
//        menuItemAircraftTools.addActionListener(this);
//
//        menuExit = new JMenuItem("Exit");
//        menuExit.addActionListener(this);
//        
//        openConnection = new JMenuItem("Open Connection");
//        openConnection.addActionListener(this);
//        
//        lookupConnection = new JMenuItem("Connect to a Game");
//        lookupConnection.addActionListener(this);
//        
//        menuItemVoice = new JMenuItem("Voice");
//        menuItemVoice.addActionListener(this);
        //mumu start 
        aircraft = new JCheckBoxMenuItem("Add");
	aircraft.addActionListener(this);

	radarToolsMenu = new JCheckBoxMenuItem("Radar Tools");
	radarToolsMenu.addActionListener(this);

	create = new JCheckBoxMenuItem("Create");
	create.addActionListener(this);
        
        cgi = new JCheckBoxMenuItem("CGI");
        cgi.addActionListener(this);

	utility = new JCheckBoxMenuItem("Utility");
	utility.addActionListener(this);

//	geofigure = new JCheckBoxMenuItem("Geometric Figure");
//	geofigure.addActionListener(this);

	gamecontrol = new JCheckBoxMenuItem("Game Control");
	gamecontrol.addActionListener(this);

	aircraftTools = new JCheckBoxMenuItem("Aircraft Tools");
	aircraftTools.addActionListener(this);

	exit = new JMenuItem("Exit");
	exit.addActionListener(this);

        loadGS = new JMenuItem("Load Game");
        loadGS.addActionListener(this);
        
        saveGS = new JMenuItem("Save Game");
        saveGS.addActionListener(this);
        
	openConnection = new JMenuItem("Open Connection");
	openConnection.addActionListener(this);

	lookupConnection = new JMenuItem("Connect to a Game");
	lookupConnection.addActionListener(this);

	voice = new JCheckBoxMenuItem("Voice");
	voice.addActionListener(this);

        mainMenuBar.add(menuFile);
        mainMenuBar.add(menuView);
        mainMenuBar.add(menuHelp);
        //mumu
        mainMenuBar.add(windowMenu);
        
        
        menuFile.add(loadGS);
        menuFile.add(saveGS);
        menuFile.add(openConnection);
        menuFile.add(lookupConnection);
        menuFile.add(exit);
        
        
	menuView.add(create);
        menuView.add(aircraft);
        menuView.add(radarToolsMenu);        
	menuView.add(aircraftTools);
        menuView.add(utility);
	menuView.add(cgi);
        menuView.add(gamecontrol);
        menuView.add(voice);
        
        
        //mumu start
        
        
      
      tileItem = new JMenuItem("Tile");
      tileItem.addActionListener(new ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               tileWindows();
            }
         });
      windowMenu.add(tileItem);
      final JCheckBoxMenuItem dragOutlineItem = new JCheckBoxMenuItem("Drag Outline");
      dragOutlineItem.addActionListener(new ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               deskMenu.setDragMode(dragOutlineItem.isSelected() ? JDesktopPane.OUTLINE_DRAG_MODE
                     : JDesktopPane.LIVE_DRAG_MODE);
            }
         });
      windowMenu.add(dragOutlineItem);
      //mumu end
        
        this.setJMenuBar(mainMenuBar);
        try {
            board.start_program();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        
        
    }
    
    
    

    
 
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        
        if (e.getSource() == aircraft) 
        {
            
               if(addCom==null)
               {
                 addCom = new Addcom(board);
                //addCom.setBounds(0, 0, deskMenu.getWidth(), 200);
                //addCom.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
                deskMenu.add(addCom);
                addCom.addVetoableChangeListener(new VetoableChangeListener()
               {
                  public void vetoableChange(PropertyChangeEvent event) throws PropertyVetoException
                 {
                    String name = event.getPropertyName();
                    Object value = event.getNewValue();

                    // we only want to check attempts to close a frame
                   if (name.equals("closed") && value.equals(true))
                  {
                   //tileWindows();
                     addCom.dispose();
                     addCom=null;

                   }
                }
              });
              int width = deskMenu.getWidth();
            //int height = deskMenu.getHeight() / 4;
            //int height=200;
            //int height=deskMenu.getWidth();
              int height=addCom.getHeight();
              addCom.reshape(nextFrameX, nextFrameY, width, height); 
              addCom.show();
              try
              {
                 addCom.setSelected(true);
              }
              catch (PropertyVetoException p)
              {
              }

             frameDistance = addCom.getHeight() - addCom.getContentPane().getHeight();
             nextFrameX += frameDistance;
             nextFrameY += frameDistance;
             if (nextFrameX + width > deskMenu.getWidth())
             {
                nextFrameX = 0;
             }     
             if (nextFrameY + height > deskMenu.getHeight())
            {
                nextFrameY = 0;
            } 
            }
  

   
        }
        else if(e.getSource() == create) 
        {
            if(createForm==null)
            {
                            
                createForm = new CreateForm();
                //createForm.setBounds(0, 0, deskMenu.getWidth(), 200);
                deskMenu.add(createForm);
            
            
               createForm.addVetoableChangeListener(new VetoableChangeListener()
              {
                  public void vetoableChange(PropertyChangeEvent event) throws PropertyVetoException
                {
                  String name = event.getPropertyName();
                  Object value = event.getNewValue();

                 // we only want to check attempts to close a frame
                if (name.equals("closed") && value.equals(true))
               {
                   //tileWindows();
                   createForm.dispose();
                   createForm=null;
               }
               }
              });
              int width = deskMenu.getWidth();
            //int height = deskMenu.getHeight() / 4;
            //int height=200;
            //int height=deskMenu.getWidth();
              int height=createForm.getHeight();
              createForm.reshape(nextFrameX, nextFrameY, width, height); 
              createForm.show();
              try
              {
                 createForm.setSelected(true);
              }
              catch (PropertyVetoException p)
              {
              }

              frameDistance = createForm.getHeight() - createForm.getContentPane().getHeight();
              nextFrameX += frameDistance;
              nextFrameY += frameDistance;
              if (nextFrameX + width > deskMenu.getWidth())
              {
                nextFrameX = 0;
              }     
              if (nextFrameY + height > deskMenu.getHeight())
              {
                nextFrameY = 0;
              }
                
            }

        }

      
        else if (e.getSource() == utility)
        {
            if(ut==null)
            {
                ut = new Utility(board);
                deskMenu.add(ut);
                ut.addVetoableChangeListener(new VetoableChangeListener()
               {
                   public void vetoableChange(PropertyChangeEvent event) throws PropertyVetoException
                  {
                     String name = event.getPropertyName();
                     Object value = event.getNewValue();

               // we only want to check attempts to close a frame
                  if (name.equals("closed") && value.equals(true))
                  {
                   //tileWindows();
                      ut.dispose();
                      ut=null;
                  }
                  }
                });
               int width = deskMenu.getWidth();
               //int height = deskMenu.getHeight() / 4;
               //int height=200;
              //int height=deskMenu.getWidth();
               int height=ut.getHeight();
               ut.reshape(nextFrameX, nextFrameY, width, height); 
               ut.show();
               try
               {
                  ut.setSelected(true);
               }
              catch (PropertyVetoException p)
              {
              }

              frameDistance = ut.getHeight() - ut.getContentPane().getHeight();
              nextFrameX += frameDistance;
              nextFrameY += frameDistance;
              if (nextFrameX + width > deskMenu.getWidth())
              {
                nextFrameX = 0;
              }     
              if (nextFrameY + height > deskMenu.getHeight())
              {
                nextFrameY = 0;
              } 
                
            }

        }
        else if(e.getSource() == cgi)
        {
            if(cgiForm == null)
            {
                cgiForm = new CGI();
                deskMenu.add(cgiForm);
                cgiForm.addVetoableChangeListener(new VetoableChangeListener()
               {
               public void vetoableChange(PropertyChangeEvent event) throws PropertyVetoException
               {
               String name = event.getPropertyName();
               Object value = event.getNewValue();

               // we only want to check attempts to close a frame
               if (name.equals("closed") && value.equals(true))
               {
                   //tileWindows();
                   cgiForm.dispose();
                   cgiForm=null;
               }
              }
              });
            int width = deskMenu.getWidth();
            //int height = deskMenu.getHeight() / 4;
            //int height=200;
            //int height=deskMenu.getWidth();
            int height=cgiForm.getHeight();
            cgiForm.reshape(nextFrameX, nextFrameY, width, height); 
            cgiForm.show();
            try
            {
            cgiForm.setSelected(true);
            }
            catch (PropertyVetoException p)
            {
             }

             frameDistance = cgiForm.getHeight() - cgiForm.getContentPane().getHeight();
             nextFrameX += frameDistance;
             nextFrameY += frameDistance;
             if (nextFrameX + width > deskMenu.getWidth())
             {
              nextFrameX = 0;
             }     
             if (nextFrameY + height > deskMenu.getHeight())
            {
            nextFrameY = 0;
            }
            }
            //cgiForm.setVisible(true);
        }
        else if (e.getSource() == exit) 
        {
            //System.out.println("aircraft");
            //     ob.setVisible(true);
            System.exit(0);
        } //rabbi 
        else if (e.getSource() == radarToolsMenu) 
        {
            if(radarTools==null)
            {
                 radarTools = new RadarTools(board);
                //radarTools.setVisible(true);
                 deskMenu.add(radarTools);
                radarTools.addVetoableChangeListener(new VetoableChangeListener()
                {
                   public void vetoableChange(PropertyChangeEvent event) throws PropertyVetoException
                  {
                     String name = event.getPropertyName();
                     Object value = event.getNewValue();

                        // we only want to check attempts to close a frame
                    if (name.equals("closed") && value.equals(true))
                    {
                   //tileWindows();
                        radarTools.dispose();
                        radarTools=null;
                    }
                  }
                });
                int width = deskMenu.getWidth();
                  //int height = deskMenu.getHeight() / 4;
                //int height=200;
                //int height=deskMenu.getWidth();
                int height=radarTools.getHeight();
                radarTools.reshape(nextFrameX, nextFrameY, width, height); 
                radarTools.show();
                try
                {
                    radarTools.setSelected(true);
                 }
                catch (PropertyVetoException p)
                {
                }

                frameDistance = radarTools.getHeight() - radarTools.getContentPane().getHeight();
                nextFrameX += frameDistance;
                nextFrameY += frameDistance;
               if (nextFrameX + width > deskMenu.getWidth())
               {
                  nextFrameX = 0;
                }     
               if (nextFrameY + height > deskMenu.getHeight())
               {
                  nextFrameY = 0;
               }
            }

       }
        else if (e.getSource() == gamecontrol) 
        { 
            if(frameGameControl==null)
            {
                frameGameControl = new Gamecontrol(board);
	        frameGameControl.setVisible(true);
                deskMenu.add(frameGameControl);
                frameGameControl.addVetoableChangeListener(new VetoableChangeListener()
                {
                     public void vetoableChange(PropertyChangeEvent event) throws PropertyVetoException
                {
                    String name = event.getPropertyName();
                    Object value = event.getNewValue();

               // we only want to check attempts to close a frame
                   if (name.equals("closed") && value.equals(true))
                  {
                   //tileWindows();
                      frameGameControl.dispose();
                      frameGameControl=null;
                   }
                }
                });
              int width = deskMenu.getWidth();
            //int height = deskMenu.getHeight() / 4;
            //int height=200;
            //int height=deskMenu.getWidth();
              int height=frameGameControl.getHeight();
              frameGameControl.reshape(nextFrameX, nextFrameY, width, height); 
              frameGameControl.show();
              try
              {
                frameGameControl.setSelected(true);
              }
              catch (PropertyVetoException p)
              {
              }

             frameDistance = frameGameControl.getHeight() - frameGameControl.getContentPane().getHeight();
             nextFrameX += frameDistance;
             nextFrameY += frameDistance;
             if (nextFrameX + width > deskMenu.getWidth())
             {
                nextFrameX = 0;
             }     
             if (nextFrameY + height > deskMenu.getHeight())
             {
               nextFrameY = 0;
             }
          }


	} 
        else if (e.getSource() == aircraftTools) 
        {
            if(frameAircraftTools==null)
            {
              frameAircraftTools=new AircraftTools(board);
	    //frameAircraftTools.setVisible(true);
              deskMenu.add(frameAircraftTools);
              frameAircraftTools.addVetoableChangeListener(new VetoableChangeListener()
             {
               public void vetoableChange(PropertyChangeEvent event) throws PropertyVetoException
               {
                  String name = event.getPropertyName();
                  Object value = event.getNewValue();

               // we only want to check attempts to close a frame
                if (name.equals("closed") && value.equals(true))
                {
                   //tileWindows();
                    frameAircraftTools.dispose();
                    frameAircraftTools=null;
                }
              }
             });
             int width = deskMenu.getWidth();
            //int height = deskMenu.getHeight() / 4;
            //int height=200;
            //int height=deskMenu.getWidth();
             int height=frameAircraftTools.getHeight();
             frameAircraftTools.reshape(nextFrameX, nextFrameY, width, height); 
             frameAircraftTools.show();
             try
             {
                frameAircraftTools.setSelected(true);
             }
             catch (PropertyVetoException p)
             {
             }

             frameDistance = frameAircraftTools.getHeight() - frameAircraftTools.getContentPane().getHeight();
             nextFrameX += frameDistance;
             nextFrameY += frameDistance;
             if (nextFrameX + width > deskMenu.getWidth())
             {
                nextFrameX = 0;
             }     
             if (nextFrameY + height > deskMenu.getHeight())
             {
                nextFrameY = 0;
             }
                
            }

	} 
        else if (e.getSource() == voice) 
        {
            if(vc==null)
            {
             vc = new Voicechat();
	   // vc.setVisible(true);
             deskMenu.add(vc);
            vc.addVetoableChangeListener(new VetoableChangeListener()
            {
               public void vetoableChange(PropertyChangeEvent event) throws PropertyVetoException
               {
                  String name = event.getPropertyName();
                  Object value = event.getNewValue();

               // we only want to check attempts to close a frame
                 if (name.equals("closed") && value.equals(true))
                 {
                   //tileWindows();
                     vc.dispose();
                     vc=null;
                 }
               }
             });
            int width = deskMenu.getWidth();
            //int height = deskMenu.getHeight() / 4;
            //int height=200;
            //int height=deskMenu.getWidth();
            int height=vc.getHeight();
            vc.reshape(nextFrameX, nextFrameY, width, height); 
            vc.show();
            try
            {
                vc.setSelected(true);
            }
            catch (PropertyVetoException p)
            {
            }

             frameDistance = vc.getHeight() - vc.getContentPane().getHeight();
             nextFrameX += frameDistance;
             nextFrameY += frameDistance;
             if (nextFrameX + width > deskMenu.getWidth())
             {
                nextFrameX = 0;
             }     
             if (nextFrameY + height > deskMenu.getHeight())
             {
                nextFrameY = 0;
             }
                
            }

	}
        else if (e.getSource() == openConnection)
        {
	    String name = gameScene.userName;



	    try 
            {
		new FCTUClient(name, servername, board);

	    } 
            catch (Exception ex) 
            {
		//System.out.println( "Error --> " + ex.getMessage());
	    }


	}
        else if (e.getSource() == lookupConnection) 
        {

            try 
            {
                gameScene.userName = InetAddress.getLocalHost().getHostName();
            }
            catch(Exception ex)
            {
                
            }
	    String name = gameScene.userName;
	    String text = name + "%client";


	    try 
            {
		new GetClientsConnection(text, servername, board);
	    } catch (Exception ex)
            {
		ex.printStackTrace();
	    }
	} 
        
        else if (e.getSource() == saveGS) 
        {
            saveGS();
        }
        
        else if (e.getSource() == loadGS) 
        {
            loadGS();
        }


	
    }
    
   
    
    public static void main(String[] args) {
        new HTBMain();
        
        
        
    }

        private void loadGS()
    {
        JFileChooser jFileChooser = new JFileChooser();
	int returnVal = jFileChooser.showOpenDialog(this);
	
	if(returnVal == JFileChooser.APPROVE_OPTION)
	{
	    File file = jFileChooser.getSelectedFile();	    
	    
	    try
	    {
		
		FileInputStream fileInputStream = new FileInputStream(file);
		ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
		HTB_MAIN.gameScene = (GameScene)objectInputStream.readObject();
                HTB_MAIN.gameScene.HTBParent = this.board;
		objectInputStream.close();				
		
	    }	    
	    catch(Exception ex)
	    {
		ex.printStackTrace();
	    }    
        }
    }
        private void saveGS()
        {
        JFileChooser jFileChooser = new JFileChooser();
	int returnVal = jFileChooser.showSaveDialog(this);
	
	if(returnVal == JFileChooser.APPROVE_OPTION)
	{
	    File file = jFileChooser.getSelectedFile();	    
	    
	    try
	    {
		file.createNewFile();		
		
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
		objectOutputStream.writeObject(HTB_MAIN.gameScene);         
		
		objectOutputStream.flush();
		objectOutputStream.close();		
	    }	    
	    catch(Exception ex)
	    {
		ex.printStackTrace();
	    }    
        }
    }

        public void tileWindows()
        {
     
        int frameCount = 0;
        for (JInternalFrame frame : deskMenu.getAllFrames())
        {
            if (!frame.isIcon()) 
            {
                frameCount++;
            }
        }
        if (frameCount == 0) 
        {
            return;
        }

      int rows = (int) Math.sqrt(frameCount);

      int cols=1;
      int extra = frameCount % rows;

      if(rows * cols < frameCount)
      {
          rows++;
      }
            int xpos=0;
      int ypos=0;
      int frameWidth=deskMenu.getWidth();
      int frameHeight=(int)(deskMenu.getHeight()/frameCount);
      
      for(JInternalFrame frame:deskMenu.getAllFrames())
      {
          if(!frame.isIcon())
          {
              try
              {
                  frame.setMaximum(false);
                  frame.reshape(xpos, ypos, frameWidth, frameHeight);
              }
              catch(PropertyVetoException e)
              {
                  
              }
          }
          ypos=ypos+frameHeight;
          if(ypos>deskMenu.getHeight())
          {
              ypos=deskMenu.getHeight()-frameHeight;
          }
              
      }
      
   }
       public void arrangeWindows()
       {
           
       }
}

